<?php
/*
Template Name: 一般列表模板
 */
    get_header();
    $pageModule = array_slice(explode('/', get_the_permalink($post)), -1)[0];
    $cat = 0;
    $perPage = 25;  //每一页显示的数量
    switch ($pageModule) {
        case 'news':
            $cat = 6;
            break;
        case 'buddhism-news':
            $cat = 8;
            break;
        case 'video':
            $cat = 10;
            break;
        case 'welfare':
            $cat = 26;
            break;
        case 'education':
            $cat = 27;
            break;
        case 'download':
            $cat = 28;
            break;
        case 'announcement':
            $cat = 9;
            break;
        case 'briefing':
            $cat = 12;
            break;
        default:
            break;
    }
?>
    <!-- 列表模块 -->
    <div class="main-container">
        <div class="wrap">
            <p class="page-title"><?php the_title(); ?></p>
            <div class="clearfix">
                <div class="main-block">
                    <ul class="main-common-list">
                        <?php
                            query_posts(array(
                                'showposts'=>$perPage,
                                'offset'=>$perPage * (max( 1, get_query_var('paged') )-1),
                                'cat'=>$cat
                            ));
                            while(have_posts()) {
                                the_post();
                        ?>
                        <li>
                            <a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a>
                            <span class="date"><?php the_time('Y-m-d'); ?></span>
                        </li>
                        <?php
                            }
                            wp_reset_query();
                        ?>
                    </ul>
                    <div class="pagination" id="pagination">
                    <?php 
                        $allsearch = new WP_Query(array(
                            'cat'=>$cat
                        ));
                        par_pagenavi($allsearch, $perPage); 
                    ?>
                    </div>
                </div>
                <div class="sidebar">
                    <div class="notice-mod hot-article-mod floor">
                        <div class="head-type02">
                            <h2>热门文章</h2>
                        </div>
                        <div class="article-main">
                        <?php
                            $queryArg = array(
                                'showposts' => 7,
                                "meta_key" => "post_views_count",
                                "orderby" => "meta_value_num",
                                "order" => "DESC",
                                "cat" => $cat
                            );
                            query_posts($queryArg);
                            if (have_posts()) {
                                the_post();
                            
                        ?>
                        <div class="article-first clearfix">
                            <a class="img-block" href="<?php the_permalink(); ?>" title="">
                                <?php the_post_thumbnail(array(120, 73)); ?>
                            </a>
                            <div class="art-right">
                                <h3><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h3>
                                <p class="date"><?php the_time('Y-m-d'); ?></p>
                            </div>
                        </div>
                        <?php
                        }
                        ?>
                        <ul>
                            <?php
                            for ($i = 0; $i < 7; $i++) {
                                if (have_posts()) {
                                    the_post();
                                ?>
                            <li>
                                <i></i>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </li>
                                <?php
                                } else {
                                    break;
                                }
                            }
                            ?>
                        </ul>
                    </div>
                    </div>
                    <?php get_template_part('template-parts/sidebar-topic', get_post_format()); ?>
                </div>
            </div>
        </div>
    </div>
<?php
    get_footer();
?>
<script>
(function() {
    var pageTotal = parseInt($('.pagination-total em').text());
    $('.pagination-go').on('click', function() {
        var val = parseInt($(this).siblings('input').val());
        if (isNaN(val) || val < 1 || val > pageTotal) {
            alert('请输入1~' + pageTotal + '之间的页码数');
            return;
        }
        var href = 'http://' + location.host + '/'+<?php echo $pageModule; ?>+'/page/' + val;
        location.href = href;
    }); 
})();
</script>
</body>
</html>